/*************************************************************************
	> File Name: BaseThreadPool.h
	> Author: 
	> Mail: 
	> Created Time: Tue 30 Jun 2015 10:24:14 AM CST
 ************************************************************************/

#ifndef _BASETHREADPOOL_H
#define _BASETHREADPOOL_H

#include <list>
#include "Mutex.h"
#include "Singleton.h"


// 最大空闲线程数
#define THREAD_MAX_IDLE_DEFAULT 15
// 最大线程总数
#define THREAD_MAX_TOTAL_DEFAULT 60

class ThreadPoolWorkItem;
class Thread;

class BaseThreadPool
{
public:
    /**
     * @brief 构造函数
     *
     * @param maxIdle 最大空闲线程数
     * @param maxTotal 最大线程总数
     */
    BaseThreadPool( int maxIdle = THREAD_MAX_IDLE_DEFAULT,
                int maxTotal = THREAD_MAX_TOTAL_DEFAULT );

    virtual ~BaseThreadPool();

    /**
     * @brief 下发任务到线程池
     *
     * @param pWorkItem 线程池任务指针
     *
     * @return
     *      - 0 成功
     *      - ... 失败的errno
     */
    virtual int postRequest( const ThreadPoolWorkItem* pWorkItem ) = 0;

    /**
     * @brief 启动线程池
     *
     * @return
     */
    virtual int start() = 0;

    /**
     * @brief 停止线程池
     *
     * @return
     */
    int stop();
    
protected:
    //can't copy
    BaseThreadPool( const BaseThreadPool& tp );
    BaseThreadPool& operator=( const BaseThreadPool& tp );

    /**
     * @brief 线程池中执行事件处理的线程
     */

protected:
    int m_nMaxIdle;
    int m_nMaxTotal;
    std::list<Thread*> m_threadList;
    Mutex m_mutex;  // to protect m_threadList;
};

//extern BaseThreadPool *g_pThreadPool;
#endif
